/*
 * Copyleft 2012 Power by Colen.
 *
 * Project: app-crm
 * Date: Apr 4, 2012
 */
package com.crm.business.web.cust.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.app.platform.base.utils.CommonUtil;
import com.app.platform.base.utils.DateUtil;
import com.app.platform.base.utils.StringUtil;
import com.crm.business.web.cust.service.CWOrderService;
import com.crm.common.exception.CrmBusinessException;
import com.crm.common.web.service.impl.WebCommonServiceImpl;

/**
 * Car Wash Order Service implementations
 * 
 * @author Colen
 * 
 */
public class CWOrderServiceImpl extends WebCommonServiceImpl implements
        CWOrderService {

    /** Logger */
    private static final Logger LOG = Logger
            .getLogger(CWOrderServiceImpl.class);

    @Override
    public void doQuery(Map<String, Object> params) {
        // query
        List<Map<String, Object>> list = getCommonDao().namingQueryForList("sql_cust_cworder_queryCWOrders", params);

        LOG.info(list.size() + " order(s) fetched!");
        if (list.size() == 0) {
            throw new CrmBusinessException("没有查询到任何记录！");
        }

        for (Map<String, Object> order : list) {
            order.put("gender_disp", getCdNmByValue("CD0001", order.get("gender")));
            order.put("car_size_type_disp", getCdNmByValue("CD0002", order.get("car_size_type")));
            // FIXME
            // order.put("svc_package_disp", getNmByValue("CD0001", order.get("svc_package")));
            order.put("pay_type_disp", getCdNmByValue("CD0003", order.get("pay_type")));
            order.put("status_disp", getCdNmByValue("CD0004", order.get("status")));
            order.put("order_type_disp", getCdNmByValue("CD0005", order.get("order_type")));
        }

        params.put("cworders", list);
    }

    @Override
    public void doAdd(Map<String, Object> params) {
        params.put("order_id", nextSeq("seq_cworder"));
        params.put("create_date", DateUtil.getCurrentDate());
        params.put("status", "1");
        params.put("order_type", "1");
        params.put("car_size_type", "2");

        // add a new order
        getCommonDao().namingUpdate("sql_cust_cworder_addNewOrder", params);
    }

    @Override
    public void showEdit(Map<String, Object> params) {
        String orderId = (String) params.get("orderId");
        if (StringUtil.isTrimEmpty(orderId)) {
            return;
        }

        Map<String, Object> qps = new HashMap<String, Object>();
        qps.put("orderId", orderId);
        // query
        List<Map<String, Object>> list = getCommonDao().namingQueryForList("sql_cust_cworder_queryCWOrders", qps);
        if (CommonUtil.isEmpty(list)) {
            throw new CrmBusinessException("该记录可能已经被删除！");
        }

        Map<String, Object> orderInfo = list.get(0);
        // store
        params.put("orderInfo", orderInfo);
    }


    @Override
    public void doEdit(Map<String, Object> params) {
        Map<String, Object> args = new HashMap<String, Object>();
        args.putAll(params);
        args.put("upd_date", DateUtil.getCurrentDate());
        getCommonDao().namingUpdate("sql_cust_cworder_updateOrder", args);
    }

    
    @Override
    public void checkMemberInfo(Map<String, Object> params) {
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("member_id", params.get("member_id"));
        
        
        
        // TODO MWH.
    }

}
